﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Expression.Service;
using T.FAS.Runtime.ScheduleJob.SPI;

namespace T.FAS.Runtime.ScheduleJob.Engine
{
    public class NoticeHelper
    {
        public static string SysParamReplace(string content, ScheduleJobSubscription subscription, TResult tResult)
        {
            if (!string.IsNullOrEmpty(content))
            {
                var totalCount = 0;
                var detailInfo = new List<string>();
                if (tResult is TCommonResult<TRecord> result)
                {
                    totalCount = GetTotal(detailInfo, result);
                }
                else if (tResult is TCommonResult<JObject> result2)
                {
                    totalCount = GetTotal(detailInfo, result2);
                }
                content = ContentReplace(content, subscription, totalCount, detailInfo);
            }
            return content;
        }
        /// <summary>
        /// 查询总数与明细
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="detailInfo"></param>
        /// <param name="result2"></param>
        /// <returns></returns>
        public static int GetTotal<T>(List<string> detailInfo, TCommonResult<T> result2)
        {
            int totalCount = Math.Max(result2.Count, result2.List.Count);
            foreach (var data in result2.List)
            {
                var detail = "";
                if (detailInfo.Count > 10)//如果明细超过十条，只显示十条明细
                    continue;
                Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(JsonConvert.SerializeObject(data));
                foreach (var property in dic.Keys)
                {
                    detail += Convert.ToString(dic[property]) + " ";
                }
                detailInfo.Add(detail);
            }

            return totalCount;
        }
        /// <summary>
        /// 替换变量
        /// </summary>
        /// <param name="content"></param>
        /// <param name="subscription"></param>
        /// <param name="totalCount"></param>
        /// <param name="detailInfo"></param>
        /// <returns></returns>
        public static string ContentReplace(string content, ScheduleJobSubscription subscription,int totalCount,List<string> detailInfo)
        {
            if (string.IsNullOrEmpty(content))
            {
                return content;
            }
            string detail = string.Join("\r\n\r\n", detailInfo);
            if(detail.Length > 2000)
            {
                detail = detail.Substring(0, 2000)+ "......\r\n\r\n";
            }
            content = content.Replace("${BusinessName}", subscription.BusinessName)
                    .Replace("${Name}", subscription.Name)
                    .Replace("${DataCount}", totalCount.ToString())
                    .Replace("${DetailData}", detail)
                    .Replace("${CurrentDateTime}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
                    .Replace("${CurrentDate}", DateTime.Now.ToString("yyyy-MM-dd"));
            return content;
        }
        public static void SendMsgToDingDing(string userId, string sendMsg)
        {
            string jsContent = $"Funcs.SendMsgToUser('{userId}', '{sendMsg}');";
            var parameters = new Dictionary<string, object>();
            var returnResult = ExprService.GetInstance.JSExecute(jsContent, parameters);
        }
    }
}
